//
//  SignatureView.m
//  BMW
//
//  Created by up72-2 on 15/10/26.
//  Copyright © 2015年 up72-2. All rights reserved.
//

#import "SignatureView.h"
#import "BZDatabase.h"
#import "AppDelegate.h"
@implementation SignatureView

@synthesize dataDic;

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (void)awakeFromNib
{
    isSign = NO;
    netManager = [[NetManager alloc] init];
    [netManager setDelegate:self];
    
    self.backgroundColor = RGBACOLOR(181, 181, 181, 0.5);
    
    
    self.aceView = [[ACEDrawingView alloc] initWithFrame:self.drawView.bounds];
    self.aceView.delegate = self;
    [self.aceView setLineColor:[UIColor blackColor]];
    [self.aceView setTag:90000];
    [self.drawView addSubview:self.aceView];
    
    self.drawView.layer.borderWidth = 1;
    self.drawView.layer.borderColor = [UIColor lightGrayColor].CGColor;
    
    
    self.resetBtn.layer.borderWidth = 1;
    self.resetBtn.layer.borderColor = [UIColor blackColor].CGColor;
    self.resetBtn.layer.cornerRadius = 3;
    self.resetBtn.layer.masksToBounds = YES;
    
    
}

- (void)setDataDic:(NSMutableDictionary *)sender {
    dataDic = [NSMutableDictionary dictionaryWithDictionary:sender];
    self.timeLabel.text = [dataDic objectForKey:@"purchaseIntention"];
    self.carLabel.text = [dataDic objectForKey:@"beInterestedCar"];
    self.areaLabel.text = [NSString stringWithFormat:@"%@ %@",[dataDic objectForKey:@"province"],[dataDic objectForKey:@"city"]];
    if (self.nameTF) {
        [self.nameTF setText:[NSString stringWithFormat:@"%@%@", [dataDic objectForKey:@"surname"], [dataDic objectForKey:@"fame"]]];
    }
    if (self.pwdTF) {
        [self.pwdTF setText:[dataDic objectForKey:@"mobilePhone"]];
    }
}

- (IBAction)resetBtn:(UIButton *)sender
{
    isSign = NO;
    [self.aceView clear];
}


- (void)drawingView:(ACEDrawingView *)view didEndDrawUsingTool:(id<ACEDrawingTool>)tool
{
    isSign = YES;
}


- (IBAction)commitBtn:(UIButton *)sender
{
    if (isSign == NO) {
        [MTool showMessage:@"请先签署协议" inView:self];
    }
    else
    {
        //截图
        UIImage *image = [self getImageFromView:self.mainView];
        
        //将图片写入内存
        [self saveImage:image];

        
        //上传信息 添加判断
        netManager.loadingText = @"信息上传中。。";
        [netManager signIn:dataDic];
    }
}

- (IBAction)cancleBtn:(UIButton *)sender
{
    //alert
    isSign = NO;
    self.hidden = YES;
}

-(UIImage *)getImageFromView:(UIView *)orgView{
    UIGraphicsBeginImageContext(orgView.bounds.size);
    [orgView.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext(); return image;
}

- (NSString *)saveImage:(UIImage *)tempImage{
    NSData* imageData;
    
    //判断图片是不是png格式的文件
    if (UIImagePNGRepresentation(tempImage)) {
        //返回为png图像。
        imageData = UIImagePNGRepresentation(tempImage);
    }else {
        //返回为JPEG图像。
        imageData = UIImageJPEGRepresentation(tempImage, 1.0);
    }
    
    NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
    
    NSString* documentsDirectory = [paths objectAtIndex:0];
    
    NSString* fullPathToFile = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@_protocolPhoto.jpg",self.pwdTF.text]];
    
    [imageData writeToFile:fullPathToFile atomically:NO];
    return fullPathToFile;
}

#pragma mark - 网络请求
- (void)requestStart:(NetManager *)request
{
    
}

- (void)requestError:(NetManager *)request error:(NSError *)error
{
    NSLog(@"sdfljsdkl");
}



- (void)requestDidFinished:(NetManager *)request result:(NSMutableDictionary *)result
{
    NSInteger status = [[NSString stringWithFormat:@"%@",[result objectForKey:@"state"]] integerValue];
    
    if (status == 0) {
        [MTool showMessage:[NSString stringWithFormat:@"%@",[result objectForKey:@"errorMsg"]] inView:self];
        return;
    }
    
    self.hidden = YES;
    isSign = NO;
    
    
    AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate;
    [MTool showMessage:@"信息保存成功" inView:app.window];
    
    if (self.back) {
        self.back();
    }
}
@end
